// pages/my/addStock/addStock.js
var app = getApp()
Page({
  data: {
    goodsInfoShow:true,
    tab:0,
    storageGoods: [],
    oldGoodsList:[],
    changeList:[],
    oldChangeList:[],
    isChange:false,
    isOldChange:false,
    stockType:'',
    addStockOrderId:'',
    addStockSkuId:'',
    agentId:'',
    accountId:''
  },
  onLoad: function (options) {
    this.setData({
      stockType:options.type
    })
    app.getMemInfo().then(res=>{
      this.setData({
        agentId:res.data.agentId,
        accountId:res.data.id
      })
      if (options.type==0) {
        this.getOrderList(0)
      }else{
        this.getOldGoodsList(1,res.data.agentId)
      }
    })
  },
  noStorageGoodsTap:function(event){
    this.setData({
      tab:0
    })
    if (this.data.stockType==0) {
      this.getOrderList(0)
    }else{
      this.getOldGoodsList(1,this.data.agentId)
    }
  },
  storageGoodsTap: function (event) {
    this.setData({
      tab: 1
    })
    if (this.data.stockType==0) {
      this.getOrderList(1)
    }else{
      this.getOldGoodsList(2,this.data.agentId)
    }
  },
  bindChange(e){
    console.log(e.currentTarget.dataset.id)
    let data = {
      id:e.currentTarget.dataset.id,
      price:e.detail.value
    }
    console.log(data)
    app.request('wx_app/agent_order/udapteAgentOrderDetail',data,'GET').then(res=>{
      console.log(res)
    })
  },
  bindChangePrice(e){
    let id = e.currentTarget.dataset.id
    console.log(id)
    this.setData({
      addStockOrderId:id
    })
    let that = this
    this.data.storageGoods.forEach(item=>{
      if (item.orderId==id) {
        that.setData({
          changeList:item.orderDetails
        })
        console.log(item.orderDetails)
      }
    })
    this.setData({
      isChange:true
    })
  },
  bindOldChangePrice(e){
    let that = this
    let id = e.currentTarget.dataset.id
    this.setData({
      addStockSkuId:id
    })
    console.log(id)
    this.data.oldGoodsList.forEach(item=>{
      if (item.id==id) {
        console.log(item)
        let oldChangeList = []
        oldChangeList.push(item)
        that.setData({
          oldChangeList
        })
      }
    })
    this.setData({
      isOldChange:true
    })
  },
  //加入库存
  addToStockTap:function(e){
    let data = {
      orderId:e.currentTarget.dataset.id
    }
    console.log(data)
    app.request('wx_app/agent_order/addSkuInfo',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        wx.showToast({
          title: '成功',
          icon: 'success',
          duration: 2000
        })
        this.setData({
          isChange:false
        })
        this.getOrderList(0)
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  watchStockTap:function(event){
    wx.switchTab({
      url: '../../stock/stock',
    })
  },
  
  getOrderList(storeState){
    console.log(333)
    let data = {
      agentId:this.data.agentId,
      accountId:this.data.accountId,
      orderState:4,
      storeState
    }
    app.request('wx_app/agent_order/getStoredOrderList',data,'GET').then(res=>{
      console.log(res)
        let storageGoods = []
        if (res.data.orders) {        
          res.data.orders.forEach(item=>{
            storageGoods.push(item)
          })
          console.log(storageGoods)
          this.setData({
            storageGoods
          })
        }else{
          this.setData({
            storageGoods:[]
          })
        }
    })
  },
  closeChange(){
    this.setData({
      isChange:false
    })
  },
  closeOldChange(){
    this.setData({
      isOldChange:false
    })
  },
  //旧商品
  getOldGoodsList(complete,agentId){
    let that = this
    let data = {
      complete,//0未完成；1已完成待加入库存;2已经加入库存
      agentId
    }
    console.log(data)
    app.request('wx_app/mall/oldForNew/oldSkuList',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {      
        let oldGoodsList = []
        res.data.pageContent.forEach(item=>{
          oldGoodsList.push({
            name:item.product_name,
            url:item.img_url1,
            type:item.versionValues,
            num:item.amount,
            price:item.stock_price,
            orderNo:item.order_no,
            id:item.id
          })
        })
        that.setData({
          oldGoodsList
        })
      }else{
        app.errorMsg(res.message)
      }
      
    })
  },
  addOld(e){
    let data = {
      oldSkus:e.currentTarget.dataset.id
    }
    console.log(data)
    app.request('wx_app/oldForNew/oldSku/saveOldSkuToSku',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        wx.showToast({
          title: '成功',
          icon: 'success',
          duration: 2000
        })
        this.setData({
          isOldChange:false
        })
        this.getOldGoodsList(1,this.data.agentId)
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  delOld(e){
    let data = {
      id:e.currentTarget.dataset.id
    }
    console.log(data)
    app.request('wx_app/oldForNew/oldSku/discarded',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        app.errorMsg('已废弃')
        this.setData({
          isOldChange:false
        })
        this.getOldGoodsList(1,this.data.agentId)
      }else{
        app.errorMsg(res.message)
      }
    })
  }
})